﻿using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
using Furion.DatabaseAccessor;
using SqlSugar;
using HiSportApplet.Application.Common.SqlSugar;
using HiSportApplet.Core.Entity.Bas;
using HiSportApplet.Application.Service.Bas.BasSportsEventsTypeManagement.Dtos;
using Furion.FriendlyException;
using HiSportApplet.Core.Util;

namespace HiSportApplet.Application.Service.Bas.BasSportsEventsTypeManagement
{
    /// <summary>
    /// 运动项目类型
    /// </summary>
    [ApiDescriptionSettings("基础资料", Tag = "运动项目类型", SplitCamelCase = false)]
    public class SportsTypeService : ITransient, ISportsTypeService, IDynamicApiController
    {

        private readonly SqlSugarRepository<BasSportsEventsType> _sportEventsTypeRep;


        public SportsTypeService(SqlSugarRepository<BasSportsEventsType> sportEventsTypeRep)
        {
            _sportEventsTypeRep = sportEventsTypeRep;
        }


        /// <summary>
        /// 查询运动项目类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>       
        [ApiDescriptionSettings(Name = "GetListPage"), HttpPost]
        [DisplayName("查询运动项目类型列表")]
        public async Task<SqlSugarPagedList<SportsTypeBasicDto>> GetListPage(SportsTypeInputDto input)
        {
            ParamNull(input);
            var data = SqlWhere(input);
            if (data == null) return new SqlSugarPagedList<SportsTypeBasicDto>();

            //排序 分页
            var result = await data.OrderBy(a => a.Seq)
                .Select(a => new SportsTypeBasicDto
                {
                    Id = a.Id,
                    Icon = a.Icon,
                    SportsEventsName = a.SportsEventsName,
                    SportsEventsCode = a.SportsEventsCode
                })
                 .ToPagedListAsync(input.Page, input.PageSize);

            return result;
        }


        #region 私有方法

        /// <summary>
        /// 运动项目类型查询条件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private ISugarQueryable<BasSportsEventsType> SqlWhere(SportsTypeInputDto input)
        {
            ParamNull(input);
            var data = _sportEventsTypeRep.AsQueryable();
            data = data.Where(a => a.IsDelete == false);

            #region 类型名称
            var name = input.SportsEventsName;
            if (!string.IsNullOrWhiteSpace(name))
            {
                data = data.Where(a => a.SportsEventsName.Contains(name));
            }
            #endregion

            #region code
            var sportsEventsCode = input.SportsEventsCode;
            if (!string.IsNullOrWhiteSpace(sportsEventsCode))
            {
                data = data.Where(a => a.SportsEventsCode.Contains(sportsEventsCode));
            }
            #endregion

            #region 停用、启用
            var status = input.Status;
            if (status != null && status.HasValue && status.Value > 0)
            {
                data = data.Where(a => a.Status == status);
            }

            #endregion 

            return data;
        }


        /// <summary>
        /// 参数为NULL验证
        /// </summary>
        /// <param name="input"></param>
        private void ParamNull(object input)
        {
            if (input == null) throw Oops.Oh("参数错误:参数为NULL");
        }

        #endregion


    }
}
